package com.easy.restful.crs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.restful.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import com.easy.restful.common.core.common.pagination.Page;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.easy.restful.sys.service.ImportService;
import com.easy.restful.util.office.ExcelUtil;
import com.easy.restful.crs.model.CrsCar;
import com.easy.restful.crs.service.CrsCarService;
import com.easy.restful.crs.dao.CrsCarMapper;

/**
 * 汽车
 *
 * @author Ljv
 * @date 2021-05-14
 */
@Service
public class CrsCarServiceImpl extends ServiceImpl<CrsCarMapper, CrsCar> implements CrsCarService ,ImportService {

    /**
     * 列表
     * @param object 查询条
     * @param page   分页
     * @return Page<CrsCar>
     */
    @Override
    public Page<CrsCar> select(CrsCar object, Page<CrsCar> page) {
        QueryWrapper<CrsCar> queryWrapper = new QueryWrapper<>();
        if(object != null){
            // 查询条件
            // 汽车名称
            if (Validator.isNotEmpty(object.getCarname())) {
                queryWrapper.like("t.carName", object.getCarname());
            }
            // 类型
            if (Validator.isNotEmpty(object.getType())) {
                queryWrapper.eq("t.type", object.getType());
            }
            // 地区
            if (Validator.isNotEmpty(object.getBrandId())) {
                queryWrapper.eq("t.brand_id", object.getBrandId());
            }
            // 配置
            if (Validator.isNotEmpty(object.getConfigId())) {
                queryWrapper.eq("t.config_id", object.getConfigId());
            }
            // 简介
            if (Validator.isNotEmpty(object.getIntro())) {
                queryWrapper.eq("t.intro", object.getIntro());
            }
            // 车辆识别码
            if (Validator.isNotEmpty(object.getVin())) {
                queryWrapper.eq("t.vin", object.getVin());
            }
            // 价格
            if (Validator.isNotEmpty(object.getBuyPrice())) {
                queryWrapper.eq("t.buy_price", object.getBuyPrice());
            }
            // 门店编号
            if (Validator.isNotEmpty(object.getShopId())) {
                queryWrapper.eq("t.shop_id", object.getShopId());
            }
            // 车辆状态(可租1，维修中2，租出3，删除4)
            if (Validator.isNotEmpty(object.getState())) {
                queryWrapper.eq("t.state", object.getState());
            }
            // 价钱
            if (Validator.isNotEmpty(object.getPrice())) {
                queryWrapper.eq("t.price", object.getPrice());
            }
            // 添加时间
            if (Validator.isNotEmpty(object.getAddtime())) {
                queryWrapper.eq("t.addtime", object.getAddtime());
            }
            // 保险ID
            if (Validator.isNotEmpty(object.getInsuranceId())) {
                queryWrapper.eq("t.insurance_id", object.getInsuranceId());
            }
        }
        page.setRecords(getBaseMapper().select(page, queryWrapper));
        return page;
    }

    /**
     * 详情
     *
     * @param id id
     * @return CrsCar
     */
    @Override
    public CrsCar input(String id) {
        ToolUtil.checkParams(id);
        return getBaseMapper().getById(id);
    }

    /**
     * 新增
     *
     * @return CrsCar
     */
    @Override
    public CrsCar add() {
        CrsCar object = new CrsCar();
        // 设置默认值
        return object;
    }

    /**
     * 删除
     *
     * @param ids 数据ids
     * @return true/false
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean remove(String ids) {
        ToolUtil.checkParams(ids);
        List<String> idList = Arrays.asList(ids.split(","));
        return removeByIds(idList);
    }

    /**
     * 保存
     *
     * @param object 表单内容
     * @return CrsCar
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public CrsCar saveData(CrsCar object) {
        ToolUtil.checkParams(object);
        if (Validator.isEmpty(object.getId())) {
            // 新增,设置默认值
        }
        return (CrsCar) ToolUtil.checkResult(saveOrUpdate(object), object);
    }

    /**
     * 验证数据，插入临时表后调用
     * 注: 返回false会触发异常回滚
     *
     * @param templateId 模板id
     * @param userId 用户id
     * @return true/false
     */
    @Override
    public boolean verificationData(String templateId, String userId) {
        return true;
    }

    /**
     * 导入前回调，插入正式表之前会调用此方法，建议导入正式表之前使用次方法再次验证数据，防止验证 ~ 导入之间数据发送变动
     * 注: 返回false会触发异常回滚
     *
     * @param templateId 模板id
     * @param userId 用户id
     * @return true/false
     */
    @Override
    public boolean beforeImport(String templateId, String userId) {
        return true;
    }

    /**
     * 导入后回调，插入正式表后会调用此方法
     * 注: 返回false会触发异常回滚
     *
     * @return true/false
     */
    @Override
    public boolean afterImport() {
        return true;
    }

    @Override
    public String exportData(CrsCar object) {
        QueryWrapper<CrsCar> queryWrapper = new QueryWrapper<>();
        if(object != null){
            // 查询条件
            // 汽车名称
            if (Validator.isNotEmpty(object.getCarname())) {
                queryWrapper.like("t.carName", object.getCarname());
            }
            // 类型
            if (Validator.isNotEmpty(object.getType())) {
                queryWrapper.eq("t.type", object.getType());
            }
            // 地区
            if (Validator.isNotEmpty(object.getBrandId())) {
                queryWrapper.eq("t.brand_id", object.getBrandId());
            }
            // 配置
            if (Validator.isNotEmpty(object.getConfigId())) {
                queryWrapper.eq("t.config_id", object.getConfigId());
            }
            // 简介
            if (Validator.isNotEmpty(object.getIntro())) {
                queryWrapper.eq("t.intro", object.getIntro());
            }
            // 车辆识别码
            if (Validator.isNotEmpty(object.getVin())) {
                queryWrapper.eq("t.vin", object.getVin());
            }
            // 价格
            if (Validator.isNotEmpty(object.getBuyPrice())) {
                queryWrapper.eq("t.buy_price", object.getBuyPrice());
            }
            // 门店编号
            if (Validator.isNotEmpty(object.getShopId())) {
                queryWrapper.eq("t.shop_id", object.getShopId());
            }
            // 车辆状态(可租1，维修中2，租出3，删除4)
            if (Validator.isNotEmpty(object.getState())) {
                queryWrapper.eq("t.state", object.getState());
            }
            // 价钱
            if (Validator.isNotEmpty(object.getPrice())) {
                queryWrapper.eq("t.price", object.getPrice());
            }
            // 添加时间
            if (Validator.isNotEmpty(object.getAddtime())) {
                queryWrapper.eq("t.addtime", object.getAddtime());
            }
            // 保险ID
            if (Validator.isNotEmpty(object.getInsuranceId())) {
                queryWrapper.eq("t.insurance_id", object.getInsuranceId());
            }
        }
        List<CrsCar> list = getBaseMapper().exportData(queryWrapper);
        return ExcelUtil.writeAndGetDownloadId("汽车", "汽车", list, CrsCar.class);
    }
}